package com.hjm.admin.moule.view.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hjm.admin.moule.viewmodel.BaseViewModel;

import butterknife.ButterKnife;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

public abstract class BaseFragment extends Fragment {
    private BaseViewModel mModel;
    protected Context mContext;
    protected View mView;
    protected CompositeDisposable mDisposable;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    public View setContentView(ViewGroup container, @LayoutRes int resId, BaseViewModel model) {
        if (mView == null) {
            mView = LayoutInflater.from(getActivity()).inflate(resId, container, false);
            ButterKnife.bind(this, mView);
            initView();
        }
        return mView;
    }

    protected abstract void initView();

    protected void addDisposable(Disposable d) {
        if (mDisposable == null) {
            mDisposable = new CompositeDisposable();
        }
        mDisposable.add(d);
    }

    public void startActivity(Class<?> className) {
        Intent intent = new Intent(mContext, className);
        startActivity(intent);
    }

    public void startActivity(Class<?> className, Bundle bundle) {
        Intent intent = new Intent(mContext, className);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mModel != null) {
            mModel.onDestroy();
        }
    }
}
